package com.intct.utils;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.apache.flink.api.java.utils.ParameterTool;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-09-19 15:05
 */
public class JedisUtil {

    private static JedisPool jedisPool;

    static {
        // 从工程中resoucres路径读取配置文件
        ParameterTool parameterTool = null;
        try {
            parameterTool = ParameterTool.fromPropertiesFile(JedisUtil.class.getResource("/intct.properties").getFile());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(100);
        poolConfig.setMaxIdle(100);
        poolConfig.setMinIdle(10);
        String hosts = parameterTool.get("redis.host", "node1");
        String port = parameterTool.get("redis.port", "3306");
        String password = parameterTool.get("redis.password", "123456");
        jedisPool = new JedisPool(poolConfig, hosts, Integer.parseInt(port), 2000, password);
    }

    public static Jedis getJedis() {
        return jedisPool.getResource();
    }
}
